/*
 * @Author: heliuxia heliuxia@163.com
 * @Date: 2023-04-03 21:51:01
 * @LastEditors: heliuxia heliuxia@163.com
 * @LastEditTime: 2023-04-16 13:39:49
 * @FilePath: /sample-chat/chat-web/src/store/modules/auth/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import {
  getToken,
  removeToken,
  setToken,
  setAuthorizationToken,
  getAuthorizationToken,
  removeAuthorizationToken,
  getTokenExpirationTime,
  setTokenExpirationTime,
  removeTokenExpirationTime,
  getUserId,
  setUserId,
  removeUserId,
} from './helper'
import { store } from '@/store'
// import { chatService } from '@/api/chat-service'

// interface loginRequst {
//   success?: boolean
//   code?: string | number
//   data?: {
//     token: string
//     userInfo: object
//   }
//
// }

export interface AuthState {
  token: string | undefined
  authorizationToken: string | undefined
  tokenExpirationTime: string | undefined
  userId: string | undefined
}

export const useAuthStore = defineStore('auth-store', {
  state: (): AuthState => ({
    token: getToken(),
    authorizationToken: getAuthorizationToken(),
    tokenExpirationTime: getTokenExpirationTime(),
    userId: getUserId(),
  }),

  getters: {},

  actions: {
    // 密钥token
    setToken(token: string) {
      this.token = token
      setToken(token)
    },

    removeToken() {
      this.token = undefined
      removeToken()
    },

    // 授权token
    setAuthorizationToken(token: string) {
      this.authorizationToken = token
      setAuthorizationToken(token)
    },

    removeAuthorizationToken() {
      this.authorizationToken = undefined
      removeAuthorizationToken()
    },

    // token过期时间
    setTokenExpirationTime(time: string) {
      this.tokenExpirationTime = time
      setTokenExpirationTime(time)
    },

    removeTokenExpirationTime() {
      this.tokenExpirationTime = undefined
      removeTokenExpirationTime()
    },

    // userId
    setUserId(userId: string) {
      this.userId = userId
      setUserId(userId)
    },

    removeUserId() {
      this.userId = undefined
      removeUserId()
    },
    //
    // // 账号登录
    // async Login<T>(params: object) {
    //   const res: loginRequst = await chatService.fetchLogin(params)
    //
    //   const { success, data, code } = res
    //   // 清空登录数据
    //   removeToken()
    //   const { token = '', userInfo = {} } = data || {}
    //   if (success) {
    //     setToken(token)
    //     useUserStore().updateUserInfo(userInfo)
    //   }
    //   return Promise.resolve({ success, code })
    // },
  },
})

// export function useAuthStoreWithout() {
//   console.log("aaa", useAuthStore(store));
//   return useAuthStore(store)
// }
